package cn.fyl.pet.controller;

import cn.fyl.basic.util.JsonResult;

import cn.fyl.basic.util.PageList;

import cn.fyl.pet.domain.Pet;
import cn.fyl.pet.query.PetQuery;
import cn.fyl.pet.service.IPetService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/pet")
public class PetController {

    @Autowired
    private IPetService PetService;



    /**
     * 查询所有
     * @return
     */
    @GetMapping
    public List<Pet> findAll(){
        return PetService.loadAll();
    }

    /**
     * 查询一个
     * @param id
     * @return
     */
    @GetMapping("/{id}")

    public Pet findById(@PathVariable("id") Long id){
        return PetService.loadById(id);
    }

    /**
     * 删除操作
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public JsonResult delete(@PathVariable("id") Long id){
        try {
            PetService.remove(id);
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("操作失败");
        }
    }

    /**
     * 添加和修改操作
     * @param Pet
     * @return
     */
    @PostMapping
    public JsonResult save(@RequestBody Pet Pet){
        try {
            if(Pet.getId()==null){
                PetService.save(Pet);
            }else {
                PetService.update(Pet);
            }
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("操作失败");
        }

    }

    /**
     * 分页查询
     * @param PetQuery
     * @return
     */
    @PutMapping
    public PageList<Pet> queryPage(@RequestBody PetQuery PetQuery){
        try {
            return PetService.queryData(PetQuery);
        } catch (Exception e) {
            e.printStackTrace();
            return new PageList<Pet>();
        }
    }

    @PatchMapping
    public JsonResult patchDelete(@RequestBody List<Long> ids){
        try {
            PetService.patchDelete(ids);
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("操作失败");
        }
    }

    @PostMapping("/onsale")
    public JsonResult onSale(@RequestBody List<Long> ids){
        try {
            PetService.onSale(ids);
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("操作失败");
        }
    }

    @PostMapping("/offsale")
    public JsonResult offSale(@RequestBody List<Long> ids){
        try {
            PetService.offSale(ids);
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("操作失败");
        }
    }


}
